//
//  AXFDetailAddressController.m
//  AXFEightGroup
//
//  Created by Haozhong Chen on 2016/12/23.
//  Copyright © 2016年 ya. All rights reserved.
//

#import "AXFDetailAddressController.h"
#import "AXFDataModel.h"
#import "AXFMapViewController.h"
#import "AXFLocationManager.h"



@interface AXFDetailAddressController ()<UIGestureRecognizerDelegate>
@property (weak, nonatomic) IBOutlet UIButton *saveButton;
@property (weak, nonatomic) IBOutlet UIButton *deleteButton;
@property (weak, nonatomic) IBOutlet UIButton *locateButton;

@property (weak, nonatomic) IBOutlet UIButton *genderMaleButton;
@property (weak, nonatomic) IBOutlet UIButton *genderFemaleButton;

@property (weak, nonatomic) IBOutlet UITextField *nameTextField;
@property (weak, nonatomic) IBOutlet UITextField *cityTextField;
@property (weak, nonatomic) IBOutlet UITextField *districtTextField;
@property (weak, nonatomic) IBOutlet UITextField *addressTextField;
@property (weak, nonatomic) IBOutlet UITextField *telephoneTextField;

@end

@implementation AXFDetailAddressController
- (IBAction)clickedSaveButton:(UIButton *)sender
{
    if ([self.delegate respondsToSelector:@selector(addAddressData:)])
    {
        _dataModel.accept_name=_nameTextField.text;
        _dataModel.city_name=_cityTextField.text;
        _dataModel.telphone=_telephoneTextField.text;
        _dataModel.address=_addressTextField.text;
        _dataModel.addr_for_dealer=_districtTextField.text;
        if (_genderMaleButton.selected==YES)
        {
            _dataModel.gender=@"1";
        }
        else
        {
            _dataModel.gender=@"0";
        }
        
        
        
        
        [self.delegate addAddressData:(_dataModel)];
        [self clickedBackButton];
    }
    else
    {
        NSLog(@"Checkout delegate");
    }
    
    
}



- (IBAction)clickedDeleteButton:(UIButton *)sender
{
    if ([self.delegate respondsToSelector:@selector(deleteAddressData)])
    {
        [self.delegate deleteAddressData];
        [self clickedBackButton];
    }
    else
    {
        NSLog(@"Checkout delegate");
    }
}

- (IBAction)clickedLocateButton:(UIButton *)sender
{
    
}

- (IBAction)clickedGenderMaleButton:(UIButton *)sender
{
    _genderMaleButton.selected=YES;
    _genderFemaleButton.selected=NO;
    [self isTextFieldHasText];
    
}
- (IBAction)clickedGenderFemaleButton:(UIButton *)sender
{
    _genderMaleButton.selected=NO;
    _genderFemaleButton.selected=YES;
    [self isTextFieldHasText];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setupUI];
    
    // Uncomment the following line to preserve selection between presentations.
    // self.clearsSelectionOnViewWillAppear = NO;
    
    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;
}


-(void)setupUI
{
    _saveButton.enabled=NO;
    [_addressTextField addTarget:self action:@selector(isTextFieldHasText) forControlEvents:UIControlEventAllEditingEvents];
    [_nameTextField addTarget:self action:@selector(isTextFieldHasText) forControlEvents:UIControlEventAllEditingEvents];
    [_telephoneTextField addTarget:self action:@selector(isTextFieldHasText) forControlEvents:UIControlEventAllEditingEvents];
    [_cityTextField addTarget:self action:@selector(isTextFieldHasText) forControlEvents:UIControlEventAllEditingEvents];
    [_districtTextField addTarget:self action:@selector(isTextFieldHasText) forControlEvents:UIControlEventAllEditingEvents];

    
    
    _nameTextField.text=_dataModel.accept_name;
    _cityTextField.text=_dataModel.city_name;
    _telephoneTextField.text=_dataModel.telphone;
    _addressTextField.text=_dataModel.address;
    _districtTextField.text=_dataModel.addr_for_dealer;
    [self handleGenderButton];
    //返回按钮
    UIButton*backButton=[[UIButton alloc]initWithFrame:CGRectZero];
    [backButton setImage:[UIImage imageNamed:@"v2_goback"] forState:UIControlStateNormal];
    [backButton setImage:[UIImage imageNamed:@"v2_goback"] forState:UIControlStateSelected];
    [backButton sizeToFit];
    [backButton addTarget:self action:@selector(clickedBackButton) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.leftBarButtonItem=[[UIBarButtonItem alloc] initWithCustomView:backButton];
    self.navigationController.interactivePopGestureRecognizer.delegate=self;
    
}

-(void)handleGenderButton
{
    NSString*genderString=_dataModel.gender;
    if ([genderString isEqualToString:@"1"])
    {
        _genderMaleButton.selected=YES;
        _genderFemaleButton.selected=NO;
        
    }
    else if([genderString isEqualToString:@"0"])
    {
        _genderFemaleButton.selected=YES;
        _genderMaleButton.selected=NO;
    }
}


-(void)clickedBackButton
{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)isTextFieldHasText
{
    if([_cityTextField hasText]&&[_nameTextField hasText]&&[_telephoneTextField hasText]&&[_addressTextField hasText]&&[_districtTextField hasText]&&(_genderMaleButton.selected||_genderFemaleButton.selected))
    {
        _saveButton.enabled=YES;
    }
    else
    {
        _saveButton.enabled=NO;
    }
    
}




/*
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:<#@"reuseIdentifier"#> forIndexPath:indexPath];
    
    // Configure the cell...
    
    return cell;
}
*/

/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

/*
#pragma mark - Table view delegate

// In a xib-based application, navigation from a table can be handled in -tableView:didSelectRowAtIndexPath:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    // Navigation logic may go here, for example:
    // Create the next view controller.
    <#DetailViewController#> *detailViewController = [[<#DetailViewController#> alloc] initWithNibName:<#@"Nib name"#> bundle:nil];
    
    // Pass the selected object to the new view controller.
    
    // Push the view controller.
    [self.navigationController pushViewController:detailViewController animated:YES];
}
*/


#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    AXFMapViewController*vc=segue.destinationViewController;
    vc.confirmBlock=^
    {
        self.addressTextField.text=[[AXFLocationManager sharedInstance].placemmarkArray.lastObject.name stringByAppendingString:@" 反正我是个后缀"];
        
    };
    
}


@end
